Notes

|

Outline

Object-oriented programming
OO design and programming promotes:
| 1. Code reuse
| 2. Designs which localize the effects of adding new data types to the program
| 3. A software model which is “closer” to the physical model (physical things are objects and (usually) not actions!). This often leads to a design which localizes the effects of a “mid-stream” change in the software specification.
Object-Oriented Paradigm
| Object-oriented programming solves some of the problems just mentioned. In contrast to the other techniques, we now have a web of interacting objects, each house-keeping its own state.
Handling problems and abstraction
| Understand problem
| Separate necessary from unnecessary details
Abstraction An example
| Let us consider the administration of employees in an institution. The head of the administration comes to you and ask you to create a program which allows to administer the employees.
| What employee information is needed by the administration?
| What tasks should be allowed?
| Employees are real persons who can be characterized with many properties; very few are:
Model is created. What is next?
| Define operations:
| create a new employee once a new person enters the institution
| delete an employee once he retired
| update an employee record
| Identify the operations which should be able to be performed on an abstract employee
| Decide to allow access to the employees' data only with associated operations
Properties of Abstract Data Types
| The data structure can only be accessed with defined operations.
| This set of operations is called interface and is exported by the entity.
| An entity with the properties just described is called an abstract data type (ADT).
Abstract data type Definition
| An abstract data type (ADT) is characterized by the following properties:
| 1. It exports a type.
| 2. It exports a set of operations. This set is called interface.
| 3. Operations of the interface are the one and only access mechanism to the type's data structure.
| 4. Axioms and preconditions define the application domain of the type.
Importance of Data Structure Encapsulation
| The principle of hiding the used data structure and to only provide a well-defined interface is known as encapsulation.
| Example: define an ADT for complex numbers
Importance of Data Structure Encapsulation
| The principle of hiding the used data structure and to only provide a well-defined interface is known as encapsulation.
| Example: define an ADT for complex numbers
Importance of Data Structure Encapsulation
Notation of ADT
| Each ADT description consists of two parts:
| Data:
| This part describes the structure of the data used in the ADT in an informal way.
| Operations:
| This part describes valid operations for this ADT, hence, it describes its interface. We use the special operation constructor to describe the actions which are to be performed once an entity of this ADT is created and destructor to describe the actions which are to be performed once an entity is destroyed. For each operation the provided arguments as well as preconditions and postconditions are given.
Notation of ADT An example
| As an example the description of the ADT Integer is presented. Let k be an integer expression:
| ADT Integer is
| Data
| A sequence of digits optionally prefixed by a plus or minus sign. We refer to this signed whole number as N.
| Operations
| constructor: Creates a new integer.
| add(k): Creates a new integer which is the sum of N and k. The postcondition for this operation is su b= N+k.
| sub(k): Similar to add, this operation creates a new integer of the difference of both integer values. => the postcondition for this operation is sum = N-k.
| set(k): Set N to k. The postcondition for this operation is N = k.
| end
Abstract data types and object-orientation
| ADTs allows the creation of instances with well-defined properties and behaviour.
| In object-orientation ADTs are referred to as classes.
| Therefore a class defines properties of objects which are the instances in an object-oriented environment.
Implementation of ADT: Step 1: Instances definition
Implementation of ADT: Step 2: set(1)
Implementation of ADT: Step 2: set(1) (Cont.)
Implementation of ADT: Step 3: add
Implementation of ADT: Step 3: add (Cont.)
Class
Class Example
Objects What is an object?
Objects How objects behave?
Objects and Messages How objects communicate?
Objects How objects communicate?
Relationships
| A-Kind-Of relationship
| Is-A relationship
| Part-Of relationship
| Has-A relationship
A-Kind-Of relationship Point class
A-Kind-Of relationship Circle class
A-Kind-Of relationship Comparison of Point and Circle
Illustration of “A-Kind-Of” relationship
Is-A relationship
Part-Of relationship
Has-a relationship
Inheritance
Inheritance and part-of relationship
Inheritance and is-a relationship
Inheritance Definition
Inheritance graph
Classes and Objects in C++
Classes Private and public
Relation between CLASS and STRUCT
| struct date
| {
| int year, month, day;
| };
| class date
| {
| public:
| date(); // constructor
| ~date(); // destructor
| int year, month, day;
| };
Classes
| The syntax for defining a function that is a member of a class outside of the actual class definition is to put the return type, then put the class name, two colons, and then the function name. This tells the compiler that the function is a member of that class.
| EXAMPLE:
| void Aclass::aFunction()
| {
| cout<<"Whatever code";
| }
Example Point class
How the methods know from which object they are invoked?
Constructors and Destructors
| Classes should always contain two functions: the constructor and destructor.
| Syntax:
| the class name denotes a constructor
| a ~ before the class name is a destructor
| The basic idea is
| to have the constructor initialises variables
| to have the destructor clean up after the class, which includes freeing any memory allocated
Constructors and Destructors
| The only time the constructor is called is when the programmer declares an instance of the class, which will automatically call the constructor.
| The only time the destructor is called is when the instance of the class is no longer needed.
| NEITHER constructors NOR destructors RETURN AN ARGUMENT! This means you do not want to try to return a value in them.
Initialisation of Point class Example
Constructors
List in classes
Destructor
Inheritance in C++
Access rights and inheritance
Example A: Class definition
Example A: Constructor, Destructor
Example A: Functions
Example A: Main() functions
Abstraction
| See Course 6, ch5.html
Encapsulation
| See Course 6, ch6.html
Task
| You are in a restaurant and you are observing the following dialog:
| Customer 1 (to waiter) Can I have a bill please?
| Customer 2 (to waiter) Can I have a bill please?
| Waiter (to Customer1) It is £24.56.
| Customer 1 (to waiter) Here you are. I am paying by Master Card.
| Waiter (to Customer 2) It is £35.34.
| Customer 2 (to waiter) Can you arrange home delivery?
| Waiter (to Customer 2) Yes, it costs £ 5.00 extra. In total it is £40.34.
| Customer 2 (to waiter) Here you are. I am paying by Switch card.
| Customer 2 (Customer 1) Home delivery is very expensive here, don’t you think so?